package com.gc.cscott.domain.contest;

import com.gc.cscott.domain.contest.numbers.TaskNumber;
import com.gc.cscott.domain.contest.numbers.TestCaseNumber;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

/**
 *
 * @author root
 */
public class Task {
    transient Contest contest;

    TaskNumber number;

    String name;

    String relativeDir;

    String inputFileName;

    String validationFileName;

    String outputFileName;

    long timeout;

    List<TestCase> testCaseList;
    transient Map<TestCaseNumber, TestCase> testCaseMap = null;

    int defaultScore = 1;

    private void initTransients(){
        if (testCaseMap==null){
            Builder<TestCaseNumber, TestCase> builder = ImmutableMap.<TestCaseNumber, TestCase>builder();
            for(TestCase testCase:testCaseList){
                builder.put(testCase.getNumber(), testCase);
            }
            testCaseMap = builder.build();
        }
    }

    public int getDefaultScore() {
        return defaultScore;
    }

    public TestCase getTestCase(TestCaseNumber testCaseNumber){
        initTransients();
        return testCaseMap.get(testCaseNumber);
    }

    public List<TestCaseNumber> getTestCaseNumbersAvailable(){
        initTransients();
        return ImmutableList.copyOf(new TreeSet<TestCaseNumber>(testCaseMap.keySet()));
    }

    public Contest getContest() {
        return contest;
    }

    public void setContest(Contest contest) {
        this.contest = contest;
    }

    public TaskNumber getNumber() {
        return number;
    }

    public String getRelativeDir() {
        return relativeDir;
    }

    public void setRelativeDir(String relativeDir) {
        this.relativeDir = relativeDir;
    }

    public List<TestCase> getTestCaseList() {
        return testCaseList;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getInputFile() {
        if (inputFileName == null) {
            return "input.txt";
        } else {
            return inputFileName;
        }
    }

    public String getValidationFile() {
        if (validationFileName == null) {
            return "output1.txt";
        } else {
            return validationFileName;
        }
    }

    public String getOutputFileName() {
        if (outputFileName==null){
            return "output.txt";
        } else {
            return outputFileName;
        }
    }

    public long getTimeout() {
        return timeout;
    }
}
